const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/', component: () => import('@/components/pages/index') },
    { path: '/login', component: () => import('@/components/pages/LoginPage') },
    { path: '/register', component: () => import('@/components/pages/RegisterPage') },
    { path: '/collect', component: () => import('@/components/pages/CollectPage') },
    { path: '/goodsInfo', component: () => import('@/components/pages/GoodsInfo') },
    { path: '/seller', component: () => import('@/components/pages/SellerPage') },
    { path: '/addGoods', component: () => import('@/components/pages/AddGoods') },
    { path: '/userInfo', component: () => import('@/components/pages/UserInfo') },
    { path: '/search', component: () => import('@/components/pages/GoodsSearch') },
    { path: '/editGoods', component: () => import('@/components/pages/EditGoods') },
    { path: '/sellerInfo', component: () => import('@/components/pages/SellerInfo') },
    { path: '/cart', component: () => import('@/components/pages/ShoppingCart') },
    { path: '/chat', component: () => import('@/components/pages/ChatRoom') },
    { path: '/community', component: () => import('@/components/pages/Community') },
    { path: '/admin', component: () => import('@/components/pages/AdminPage') },
  ]
})

router.beforeEach((to, from, next) => {
  if (!localStorage.getItem('token') && to.path !== '/') {
    if (to.path === '/login' || to.path === '/register') {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
